package com.xdwanj.dao.impl;

import com.mysql.jdbc.StringUtils;
import com.xdwanj.dao.BaseDao;
import com.xdwanj.dao.ShoppingCartDao;
import com.xdwanj.pojo.Commodity;
import com.xdwanj.pojo.ShoppingCart;
import com.xdwanj.pojo.User;
import com.xdwanj.util.PageBean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ShoppingCartDaoImpl implements ShoppingCartDao {
    private BaseDao baseDao = new BaseDao();
    JdbcTemplate jdbcTemplate = baseDao.getJdbcTemplate();

    @Override
    public PageBean<ShoppingCart> findShoppingCartByUser(String Name, int userId, int currentPageNo, int pageSize) {
        List<Object> params = new ArrayList<>();
        params.add(userId);
        StringBuffer sql = new StringBuffer(" WHERE u.id=us.u_id AND us.u_id=s.u_id AND c.id=cs.c_id AND cs.c_id=s.c_id AND s.id=us.s_id and cs.s_id=s.id AND u.id=?");
        if (!StringUtils.isNullOrEmpty(Name)) {
            sql.append(" and c.name like'%").append(Name).append("%'");
        }
        System.out.println(new StringBuffer("select *,s.id shoppingId from user u,user_shopping_cart us,commodity c,commodity_shoppingcart cs,shopping_cart s").append(sql));
        System.out.println(new StringBuffer("select count(*) from user u,user_shopping_cart us，commodity c ,commodity_shoppingcart cs, shopping_cart s").append(sql));
        PageBean<ShoppingCart> customerPageDto = baseDao.getCustomerPageDto(new StringBuffer("select *,s.id shoppingId from user u,user_shopping_cart us,commodity c,commodity_shoppingcart cs,shopping_cart s").append(sql).toString(),
                params.toArray(),
                new BeanPropertyRowMapper<ShoppingCart>(ShoppingCart.class),
                new StringBuffer("select count(*) from user u,user_shopping_cart us,commodity c ,commodity_shoppingcart cs, shopping_cart s").append(sql).toString(), params.toArray(), currentPageNo, pageSize);
        List<ShoppingCart> shoppingCarts = customerPageDto.getContent();
        List<Commodity> commodities = jdbcTemplate.query(new StringBuffer("select *,s.id shoppingCartId from user u,shopping_cart s,user_shopping_cart us, commodity_shoppingcart cs, commodity c").append(sql).toString(), new BeanPropertyRowMapper<Commodity>(Commodity.class), params.toArray());
        List<User> users = jdbcTemplate.query(new StringBuffer("select *,s.id shoppingId from shopping_cart s,user_shopping_cart us,commodity c , commodity_shoppingcart cs , user u").append(sql).toString(), new BeanPropertyRowMapper<User>(User.class), params.toArray());
        for (ShoppingCart shoppingCart : shoppingCarts) {
            for (int i = 0; i < commodities.size(); i++) {
                if (shoppingCart.getId().equals(commodities.get(i).getShoppingCartId())) {
                    shoppingCart.setCommodity(commodities.get(i));
                }
                shoppingCart.setUser(users.get(i));
            }
        }
        return customerPageDto;
    }

    @Override
    public ShoppingCart findById(int id) throws Exception {
        String sql = "select *,s.id shoppingId,c.id commodityId from user u,user_shopping_cart us,commodity c,commodity_shoppingcart cs,shopping_cart s WHERE u.id=us.u_id AND us.u_id=s.u_id AND c.id=cs.c_id AND cs.c_id=s.c_id AND s.id=us.s_id and cs.s_id=s.id AND s.id=?";
        ShoppingCart shoppingCart = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<ShoppingCart>(ShoppingCart.class), id);
        return shoppingCart;
    }

    @Override
    public List<ShoppingCart> findByUserId(int uid) throws Exception {
        String sql = "select *,s.id shoppingId,c.id commodityId from user u,user_shopping_cart us,commodity c,commodity_shoppingcart cs,shopping_cart s WHERE u.id=us.u_id AND us.u_id=s.u_id AND c.id=cs.c_id AND cs.c_id=s.c_id AND s.id=us.s_id and cs.s_id=s.id AND u.id=?";
        List<ShoppingCart> shoppingCarts = jdbcTemplate.query(sql, new BeanPropertyRowMapper<ShoppingCart>(ShoppingCart.class), uid);
        return shoppingCarts;
    }

    @Override
    public void addShoppingCart(int uid, int coid, int count) throws Exception {
        String sql = "insert into shopping_cart (u_id,c_id,commodity_num,add_date) values (?,?,?,?)";
        String sql1 = "insert into commodity_shoppingcart (c_id) values (?)";
        String sql2 = "insert into user_shopping_cart (u_id) values (?)";
        jdbcTemplate.update(sql, uid, coid, count, new Date());
        jdbcTemplate.update(sql1, coid);
        jdbcTemplate.update(sql2, uid);
    }

    @Override
    public int updateShoppingCart(int uid, int comId, int num) throws Exception {
        String sql = "update shopping_cart set commodity_num =? where c_id=? and U_id=?";
        int updateRow = jdbcTemplate.update(sql, num, comId, uid);
        return updateRow;
    }


    @Override
    public void deleteShoppingCart(int id, int uid) throws Exception {
        String sql = "delete from shopping_cart where id = ? and u_id = ?";
        String sql1 = "delete from user_shopping_cart where s_id = ? and u_id = ?";
        String sql2 = "delete from commodity_shoppingcart where s_id =? ";
        jdbcTemplate.update(sql, id, uid);
        jdbcTemplate.update(sql1, id, uid);
        jdbcTemplate.update(sql2, id);
    }
}
